import HTTP from "@/common/service/httpService";
import store from "@/Redux/Store/Store";
import yhTools from "@tools/yh";
import { Toast } from "antd-mobile";
import { mockUrl } from "@config/root";
const appPlatFormData = store.getState().getAppPlatformsDatas;

// 添加一个请求拦截器
HTTP.interceptors.request.use(function (res) {
	console.log("请求拦截前res", res);
	res.headers["Auth-Header"] = appPlatFormData.authHeaderStr;
	let _root = appPlatFormData.httpUrl || "";
	if (process.env.ENV_CONFIG === "mock") {
		_root = mockUrl;
	}
	let url = "";

	if (res.url.match(/:[/]{2,3}/)) {
		url = res.url;
	} else {
		url = _root + res.url;
	}
	res.url = url;
	return res;
});

// 添加一个响应拦截器
HTTP.interceptors.response.use(function (res) {
	console.log("响应拦截前res", res);
	if (res.code) {
		if (res.code === "0") {
			console.log("success");
		} else {
			if (res.message) {
				Toast.info(res.message, 2);
			} else {
				Toast.info("系统异常", 2);
			}
			// 用户信息失效
			if (res.code === "99998" || res.code === "99999") {
				//留时间Toast展示
				setTimeout(() => {
					yhTools.closePage();
				}, 2000);
			}
		}
	} else if (res.message) {
		Toast.info(res.message, 2);
	} else {
		Toast.info("当前网络异常", 2);
	}
	return res;
});
